document.write('<link type="text/css" rel="stylesheet" href="' + context
		+ '/app/cell/css/sellist.css"/>');

var pageSize = 15;

/**
 * 监理标段(标段选项)选择(会话用户授权访问的监理标段信息)
 * 
 * @param {}
 *            combolabel
 * @param {}
 *            returnIdField
 * @param {}
 *            returnNameField
 * @param {}
 *            defaultValue
 * @param {}
 *            allowBlank
 * @return {}
 */
function selitemlist(combolabel, returnIdField, returnNameField,
		returnItemIdField, returnItemNameField, defaultValue, allowBlank,
		userid) {
	var comboxid = returnIdField + '_';

	var comboWithTooltip = new Ext.form.ComboBox({
				store : new Ext.data.SimpleStore({
							fields : [],
							data : [[]]
						}),
				fieldLabel : combolabel,
				editable : false,
				id : comboxid,
				name : comboxid,
				mode : 'local',
				triggerAction : 'all',
				listWidth : 600,
				maxHeight : 400,
				tpl : '<div id="cellComboListArea"></div>',
				selectedClass : '',
				onSelect : Ext.emptyFn,
				allowBlank : (allowBlank == true),
				emptyText : '请选择...'
			});

	var cellListStore = new Ext.data.JsonStore({
				idProperty : 'ID',
				remoteSort : false,
				totalProperty : 'totalCount',
				root : 'queryList',
				fields : ['PAGINATION_NUMBER', 'ID', 'SVBD_NAME', 'SVBD_GROUP',
						'C_NAME', 'C_TYPE', 'C_AREA', 'CB_ID', 'CB_NAME'],

				// load using script tags for cross domain, if the data in on
				// the same domain as
				// this page, an HttpProxy would be better
				proxy : new Ext.data.ScriptTagProxy({
							url : context + '/app/jlbdaction.do?method=mylist'
						})
			});

	var cellListGrid = new Ext.grid.GridPanel({
		id : 'cellListGrid',
		title : '设计单元-标段列表',
		store : cellListStore,
		columns : [{
					header : '监理标段',
					width : 200,
					sortable : true,
					dataIndex : 'SVBD_NAME'
				}, {
					header : '设计单元',
					width : 150,
					sortable : true,
					dataIndex : 'C_NAME'
				}, {
					header : '建管模式',
					width : 100,
					sortable : true,
					dataIndex : 'C_TYPE'
				}, {
					header : '标段名称',
					width : 150,
					sortable : true,
					dataIndex : 'CB_NAME',
					renderer : function(value, cellmeta, record, rowIndex,
							columnIndex, store) {
						var str = value;

						if (value != '') {
							str = '';
							var ids = record.data.CB_ID.split(',');
							var names = value.split(',');

							for (var i = 0; i < names.length; i++) {
								if (names[i] != '') {
									str += '<input type="radio" name="rad" bdName="'
											+ record.data.SVBD_NAME
											+ '" bdId="'
											+ record.data.ID
											+ '" bdItemName="'
											+ names[i]
											+ '" value="'
											+ ids[i]
											+ '" style="vertical-align:middle">'
											+ names[i] + '<br/>';
								}
							}
						}

						return str;
					}
				}],
		stripeRows : true,
		autoExpandColumn : 'SVBD_NAME',
		region : 'center',
		layout : 'fit',
		height : 250,
		border : false,
		viewConfig : {
			forceFit : true
		},
		// config options for stateful behavior
		stateful : true,
		stateId : 'grid',
		loadMask : {
			msg : "数据加载中，请稍等..."
		},
		bbar : new Ext.PagingToolbar({
					pageSize : pageSize,// 每页显示的记录值
					store : cellListStore,
					displayInfo : true,
					displayMsg : '总记录数 {0} - {1} of {2}',
					emptyMsg : "没有记录"
				}),
		listeners : {
			rowclick : function(grid, rowIndex, e) {
				var record = grid.getStore().getAt(rowIndex);

				var name = record.data.SVBD_NAME;

				/* 监理标段ID */
				var id = record.data.ID;

				var bds = A('bdId', id);

				var bdObj;

				for (var i = 0; i < bds.length; i++) {
					if (bds[i].checked) {
						bdObj = bds[i];
						break;
					}
				}

				if (bdObj) {
					comboWithTooltip.setValue(bdObj.getAttribute('bdName')
							+ ' : ' + bdObj.getAttribute('bdItemName'));
					if (Ext.getCmp(returnIdField)) {
						Ext.getCmp(returnIdField).setValue(bdObj
								.getAttribute('bdId'));

					}

					if (Ext.getCmp(returnNameField)) {
						Ext.getCmp(returnNameField).setValue(bdObj
								.getAttribute('bdName'));
					}

					if (Ext.getCmp(returnItemIdField)) {
						Ext.getCmp(returnItemIdField).setValue(bdObj.value);

					}

					if (Ext.getCmp(returnItemNameField)) {
						Ext.getCmp(returnItemNameField).setValue(bdObj
								.getAttribute('bdItemName'));
					}

					comboWithTooltip.collapse();
				}
			}
		}
	});

	cellListStore.on('load', function(store, records, options) {
				var arr = [];
				for (var i = 0; i < records.length; i++) {
					var record = records[i];
					var isMatch = hasSelectedItem(Ext.getCmp(returnIdField)
									.getValue(), record.data.ID, Ext
									.getCmp(returnItemIdField).getValue());
					if (isMatch) {
						arr.push(record);
					}
				}
				cellListGrid.getSelectionModel().selectRecords(arr);
			}, this, {
				delay : 1000
			})

	comboWithTooltip.on('expand', function() {
				cellListGrid.render('cellComboListArea');
				cellListStore.load({
							params : {
								start : 0,
								limit : pageSize
							}
						});
			});

	return comboWithTooltip;
}

/**
 * 标段选择(会话用户授权访问的监理标段信息)
 * 
 * @param {}
 *            combolabel
 * @param {}
 *            returnIdField
 * @param {}
 *            returnNameField
 * @param {}
 *            defaultValue
 * @param {}
 *            allowBlank
 * @return {}
 */
function sellist(combolabel, returnIdField, returnNameField, defaultValue,
		allowBlank, userid) {
	var comboxid = returnIdField + '_';

	var comboWithTooltip = new Ext.form.ComboBox({
				store : new Ext.data.SimpleStore({
							fields : [],
							data : [[]]
						}),
				fieldLabel : combolabel,
				editable : false,
				id : comboxid,
				name : comboxid,
				mode : 'local',
				triggerAction : 'all',
				listWidth : 600,
				maxHeight : 400,
				tpl : '<div id="cellComboListArea"></div>',
				selectedClass : '',
				onSelect : Ext.emptyFn,
				allowBlank : (allowBlank == true),
				emptyText : '请选择...'
			});

	var cellListStore = new Ext.data.JsonStore({
				idProperty : 'ID',
				remoteSort : false,
				totalProperty : 'totalCount',
				root : 'queryList',
				fields : ['PAGINATION_NUMBER', 'ID', 'SVBD_NAME', 'SVBD_GROUP',
						'C_NAME', 'C_TYPE', 'C_AREA', 'CB_ID', 'CB_NAME'],

				// load using script tags for cross domain, if the data in on
				// the same domain as
				// this page, an HttpProxy would be better
				proxy : new Ext.data.ScriptTagProxy({
							url : context + '/app/jlbdaction.do?method=mylist'
						})
			});

	var cellListGrid = new Ext.grid.GridPanel({
				id : 'cellListGrid',
				title : '设计单元-标段列表',
				store : cellListStore,
				columns : [{
							header : '监理标段',
							width : 200,
							sortable : true,
							dataIndex : 'SVBD_NAME'
						}, {
							header : '设计单元',
							width : 150,
							sortable : true,
							dataIndex : 'C_NAME'
						}, {
							header : '建管模式',
							width : 100,
							sortable : true,
							dataIndex : 'C_TYPE'
						}, {
							header : '标段名称',
							width : 150,
							sortable : true,
							dataIndex : 'CB_NAME',
							renderer : renderBDHeader
						}],
				stripeRows : true,
				autoExpandColumn : 'SVBD_NAME',
				region : 'center',
				layout : 'fit',
				height : 250,
				border : false,
				viewConfig : {
					forceFit : true
				},
				// config options for stateful behavior
				stateful : true,
				stateId : 'grid',
				loadMask : {
					msg : "数据加载中，请稍等..."
				},
				bbar : new Ext.PagingToolbar({
							pageSize : pageSize,// 每页显示的记录值
							store : cellListStore,
							displayInfo : true,
							displayMsg : '总记录数 {0} - {1} of {2}',
							emptyMsg : "没有记录"
						}),
				listeners : {
					rowclick : function(grid, rowIndex, e) {
						var record = grid.getStore().getAt(rowIndex);

						var name = record.data.SVBD_NAME;

						/* 监理标段ID */
						var id = record.data.ID;

						comboWithTooltip.setValue(name);
						if (Ext.getCmp(returnIdField)) {
							Ext.getCmp(returnIdField).setValue(id);

						}

						if (Ext.getCmp(returnNameField)) {
							Ext.getCmp(returnNameField).setValue(name);
						}

						comboWithTooltip.collapse();
					}
				}
			});

	cellListStore.on('load', function(store, records, options) {
				var arr = [];
				for (var i = 0; i < records.length; i++) {
					var record = records[i];
					var isMatch = hasSelected(Ext.getCmp(returnIdField)
									.getValue(), record.data.ID);
					if (isMatch) {
						arr.push(record);
					}
				}
				cellListGrid.getSelectionModel().selectRecords(arr);
			}, this, {
				delay : 1000
			})

	comboWithTooltip.on('expand', function() {
				cellListGrid.render('cellComboListArea');
				cellListStore.load({
							params : {
								start : 0,
								limit : pageSize
							}
						});
			});

	return comboWithTooltip;
}

function renderBDHeader(value, cellmeta, record, rowIndex, columnIndex, store) {
	var str = value;

	if (value != '') {
		str = '';
		var names = value.split(",");

		for (var i = 0; i < names.length; i++) {
			if (names[i] != '') {
				str += names[i] + '<br/>';
			}
		}
	}

	return str;
}

function renderBDItemHeader(value, cellmeta, record, rowIndex, columnIndex,
		store) {
	var str = value;

	if (value != '') {
		str = '';
		var ids = record.data.CB_ID;
		var names = value.split(",");

		for (var i = 0; i < names.length; i++) {
			if (names[i] != '') {
				str += '<input type="radio" name="rad" bdName="'
						+ record.data.SVBD_NAME
						+ '" bdId="'
						+ record.data.ID
						+ '" bdItemName="'
						+ names[i]
						+ '" value="'
						+ ids[i]
						+ '" style="vertical-align:middle" onClick="javascript:checkBDItem(this);">'
						+ names[i] + '<br/>';
			}
		}
	}

	return str;
}

/**
 * 标段选择
 * 
 * @param {}
 *            combolabel
 * @param {}
 *            returnIdField
 * @param {}
 *            returnNameField
 * @param {}
 *            defaultValue
 * @param {}
 *            allowBlank
 * @return {}
 */
function querysellist(combolabel, returnIdField, returnNameField, defaultValue,
		allowBlank) {
	var comboxid = returnIdField + '_';

	var comboWithTooltip = new Ext.form.ComboBox({
				store : new Ext.data.SimpleStore({
							fields : [],
							data : [[]]
						}),
				fieldLabel : combolabel,
				editable : false,
				id : comboxid,
				name : comboxid,
				mode : 'local',
				triggerAction : 'all',
				listWidth : 600,
				maxHeight : 400,
				tpl : '<div id="cellComboListArea"></div>',
				selectedClass : '',
				onSelect : Ext.emptyFn,
				allowBlank : (allowBlank == true),
				emptyText : '请选择...'
			});

	var cellListStore = new Ext.data.JsonStore({
				idProperty : 'ID',
				remoteSort : false,
				totalProperty : 'totalCount',
				root : 'queryList',
				fields : ['PAGINATION_NUMBER', 'ID', 'SVBD_NAME', 'SVBD_GROUP',
						'C_NAME', 'C_TYPE', 'C_AREA', 'CB_ID', 'CB_NAME'],

				// load using script tags for cross domain, if the data in on
				// the same domain as
				// this page, an HttpProxy would be better
				proxy : new Ext.data.ScriptTagProxy({
							url : context + '/app/jlbdaction.do?method=mylist'
						})
			});

	cellListStore.on('load', function(store, records, options) {
				var arr = [];
				for (var i = 0; i < records.length; i++) {
					var record = records[i];
					var isMatch = hasSelected(Ext.getCmp(returnIdField)
									.getValue(), record.data.C_ID);
					if (isMatch) {
						arr.push(record);
					}
				}
				cellListGrid.getSelectionModel().selectRecords(arr);
			}, this, {
				delay : 1000
			})

	var cellListGrid = new Ext.grid.GridPanel({
				id : 'cellListGrid',
				title : '设计单元-标段列表',
				store : cellListStore,
				columns : [{
							header : '监理标段',
							width : 200,
							sortable : true,
							dataIndex : 'SVBD_NAME'
						}, {
							header : '设计单元',
							width : 150,
							sortable : true,
							dataIndex : 'C_NAME'
						}, {
							header : '建管模式',
							width : 100,
							sortable : true,
							dataIndex : 'C_TYPE'
						}, {
							header : '标段名称',
							width : 150,
							sortable : true,
							dataIndex : 'CB_NAME',
							renderer : renderBDHeader
						}],
				stripeRows : true,
				autoExpandColumn : 'SVBD_NAME',
				region : 'center',
				layout : 'fit',
				height : 250,
				border : false,
				viewConfig : {
					forceFit : true
				},
				// config options for stateful behavior
				stateful : true,
				stateId : 'grid',
				loadMask : {
					msg : "数据加载中，请稍等..."
				},
				bbar : new Ext.PagingToolbar({
							pageSize : pageSize,// 每页显示的记录值
							store : cellListStore,
							displayInfo : true,
							displayMsg : '总记录数 {0} - {1} of {2}',
							emptyMsg : "没有记录"
						}),
				listeners : {
					rowclick : function(grid, rowIndex, e) {
						var record = grid.getStore().getAt(rowIndex);

						var name = record.data.SVBD_NAME;

						/* 监理标段ID */
						var id = record.data.ID;

						comboWithTooltip.setValue(name);
						if (Ext.getCmp(returnIdField)) {
							Ext.getCmp(returnIdField).setValue(id);

						}

						if (Ext.getCmp(returnNameField)) {
							Ext.getCmp(returnNameField).setValue(name);
						}

						comboWithTooltip.collapse();
					}
				}
			});

	comboWithTooltip.on('expand', function() {
				cellListGrid.render('cellComboListArea');
				cellListStore.load({
							params : {
								start : 0,
								limit : pageSize
							},
							callback : function(r, options, success) {
								if (success == false) {
									Ext.Msg.alert('ERROR', '加载数据出现异常.');
								} else {

								}
							}
						});
			});

	return comboWithTooltip;
}

function hasSelected(ids, recordId) {
	return ids == recordId;
}

function hasSelectedItem(ids, recordId, itemId) {
	var rnt = (ids == recordId);
	if (rnt) {
		var bds = A('bdId', ids);

		var bdObj;

		for (var i = 0; i < bds.length; i++) {
			if (bds[i].value == itemId) {
				bds[i].checked = true;
				break;
			}
		}
	}

	return rnt;
}